package com.link.base.base.sms.service;

import com.link.base.sms.model.SmsAccntLine;
import com.link.base.base.sms.dao.mybatis.mapper.SmsAccntLineMapper;
import com.link.base.base.sms.model.SendSmsAccount;
import com.link.base.base.sms.model.SmsAccntLineDTO;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: liulu
 * @Date:15:37 2018/11/5
 */

@Service
public class SmsAccntLineServiceImpl extends BasicServiceImpl<SmsAccntLine> implements SmsAccntLineService {
    @Resource
    private SmsAccntLineMapper smsAccntLineMapper;
    @Resource
    private SendSmsAccountService sendSmsAccountService;

    @Override
    public BasicMapper<SmsAccntLine> getBasicMapper() throws Exception {
        return smsAccntLineMapper;

    }

    @Override
    public SmsAccntLine existAccnt(SmsAccntLine record) {

        return smsAccntLineMapper.existAccnt(record);
    }

    @Override
    public void importAccntLineUpdate(SmsAccntLine record) {

        smsAccntLineMapper.importAccntLineUpdate(record);
    }


    @Override
    public void afterInsert(SmsAccntLine entity) throws Exception {
        SendSmsAccount sendSmsAccount = new SendSmsAccount();
        sendSmsAccount.setId(keyGenerateService.keyGenerate());
        sendSmsAccount.setAccountId(entity.getId());
        sendSmsAccount.setSmsId(Long.parseLong(entity.getxAttr69()));

        try {
            sendSmsAccountService.insert(sendSmsAccount);
        } catch (Exception e) {
            LCLogger.withException(e);
        }

    }

    /**
     * 根据短信ID查询客户分页
     */
    @Override
    public List<SmsAccntLine> queryBySendSmsIdExamplePage(SmsAccntLine account) throws Exception {


        return smsAccntLineMapper.queryBySendSmsIdExamplePage(account);
    }


    /**
     * 根据条件筛选客户
     */
    @Override
    public List<SmsAccntLine> queryByFiltrateExamplePage(SmsAccntLineDTO account) throws Exception {
        return smsAccntLineMapper.queryByFiltrateExamplePage(account);
    }

    /**
     * @author liulu
     * @zhushi: 去重
     */
    @Override
    public List<SmsAccntLine> isRepetition(List<SmsAccntLine> arr, List<SmsAccntLine> brr) {
        if ((!CollectionUtils.isEmpty(arr)) && (!CollectionUtils.isEmpty(brr))) {
            for (SmsAccntLine a1 : arr) {
                for (SmsAccntLine b1 : brr) {
                    if (StringUtils.equals(a1.getMobilePhone1(), b1.getMobilePhone1())) {
                        brr.remove(b1);

                    }
                }
            }
        }

        return brr;
    }

    /**
     * @author liulu
     * @zhushi: 去重添加
     */
    @Override
    public List<SmsAccntLine> addlist(List<SmsAccntLine> arr, List<SmsAccntLine> brr) {
        if (brr.isEmpty()) {
            return arr;
        }
        Set<String> telSet = new HashSet<>();
        for (SmsAccntLine smsAccntLine : arr) {
            if (StringUtils.isBlank(smsAccntLine.getMobilePhone1())) {
                continue;
            }
            telSet.add(smsAccntLine.getMobilePhone1());
        }
        for (SmsAccntLine smsAccntLine : brr) {
            if (StringUtils.isBlank(smsAccntLine.getMobilePhone1())) {
                continue;
            }
            boolean unExisted = telSet.add(smsAccntLine.getMobilePhone1());
            if (unExisted) {
                arr.add(smsAccntLine);
            }
        }
        return arr;
    }

    @Override
    public List<SmsAccntLine> accntLinePage(QueryParams qps, List<SmsAccntLine> list) {
        List<SmsAccntLine> arr = new ArrayList<>();
        if (qps.getPageFlag()) {
            Integer page = qps.getPage();
            Integer rows = qps.getRows();
            int count = 0;
            if ((page * rows) < list.size()) {
                count = page * rows;
            } else {
                count = list.size();
            }
            for (int i = (page - 1) * rows; i < count; i++) {
                arr.add(list.get(i));
            }
            return arr;
        } else {
            return list;
        }

    }

    @Override
    public List<SmsAccntLine> filterWxAccntByExamplePage(SmsAccntLine account) throws Exception {
        List<SmsAccntLine> list = smsAccntLineMapper.filterWxAccntByExamplePage(account);
        return list;
    }
}
